﻿using System;

namespace OnHook.Common.UtilsHelper
{
    /// <summary>
    /// 时间操作的帮助类
    /// </summary>
    public static class DateTimeHelper
    {
        /// <summary>
        /// 时间转时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ConvertTimeToStamp(DateTime dt)
        {
            //System.DateTime startTime = TimeZoneInfo.Local.GetUtcOffset().ToLocalTime(new System.DateTime(1970, 1, 1));
            DateTime dtStart = new DateTime(1970, 1, 1).Add(TimeZoneInfo.Local.BaseUtcOffset);
            return (long)(dt - dtStart).TotalSeconds;
        }

        /// <summary>
        /// 时间戳转时间
        /// </summary>
        /// <param name="times">时间戳（秒）</param>
        /// <returns></returns>
        public static DateTime ConvertStampToTime(long times)
        {
            DateTime dtStart =  new DateTime(1970, 1, 1).Add(TimeZoneInfo.Local.BaseUtcOffset); //TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//当前时区的时间
            long lTime = long.Parse(times + "0000000");
            var toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
        }

        /// <summary>
        /// 获取中文得星期几
        /// </summary>
        /// <param name="dayOfWeek"></param>
        /// <returns></returns>
        public static string GetWeekText(DayOfWeek dayOfWeek)
        {
            string text = string.Empty;
            switch (dayOfWeek)
            {
                case DayOfWeek.Sunday: text = "周日"; break;
                case DayOfWeek.Monday: text = "周一"; break;
                case DayOfWeek.Tuesday: text = "周二"; break;
                case DayOfWeek.Wednesday: text = "周三"; break;
                case DayOfWeek.Thursday: text = "周四"; break;
                case DayOfWeek.Friday: text = "周五"; break;
                case DayOfWeek.Saturday: text = "周六"; break;
            }
            return text;
        }

        /// <summary>
        /// 某年某月第几个星期几
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="weekOfmonth">第几个星期</param>
        /// <param name="dayofweek"><星期几</param>
        /// <returns></returns>
        public static DateTime GetDate(int year, int month, int weekOfmonth, DayOfWeek dayofweek)
        {
            var firstDay = new DateTime(year, month, 1);//本月第一天
            var firstDayOfWeek = firstDay.DayOfWeek; //本月第一天星期几
            if (firstDayOfWeek == dayofweek)//如果第一天和需要的那一天是同一星期，直接加周期时间
            {
                return firstDay.AddDays((weekOfmonth - 1) * 7);
            }
            else if (firstDayOfWeek < dayofweek)
            {
                int dayCount = (weekOfmonth - 1) * 7 + (dayofweek - firstDayOfWeek);
                return firstDay.AddDays(dayCount);
            }
            else
            {
                int dayCount = (weekOfmonth - 1) * 7 + (7 + dayofweek - firstDayOfWeek);
                return firstDay.AddDays(dayCount);
            }
        }

        /// <summary>
        /// 某年某月倒数第几个星期几
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="weekOfmonth">第几个星期（负数）</param>
        /// <param name="dayofweek">星期几</param>
        /// <returns></returns>
        public static DateTime GetDate2(int year, int month, int weekOfmonth, DayOfWeek dayofweek)
        {
            var firstDay = new DateTime(year, month, 1);//本月第一天
            var lastDay = firstDay.AddMonths(1).AddDays(-1);//本月最后一天
            var lastDayOfWeek = lastDay.DayOfWeek; //本月第一天星期几
            if (lastDayOfWeek == dayofweek)//如果第一天和需要的那一天是同一星期，直接加周期时间
            {
                return firstDay.AddDays((weekOfmonth + 1) * 7);
            }
            else if (lastDayOfWeek < dayofweek)
            {
                int dayCount = (weekOfmonth + 1) * 7 + (dayofweek - lastDayOfWeek);
                return firstDay.AddDays(dayCount);
            }
            else
            {
                int dayCount = (weekOfmonth + 1) * 7 + (7 + dayofweek - lastDayOfWeek);
                return firstDay.AddDays(dayCount);
            }
        }

        /// <summary>
        /// 获得一个月有多少天
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public static int GetMonthDays(int year, int month)
        {

            int count = 0;
            switch (month)
            {
                case 2: count = IsLeapYear(year) ? 29 : 28; break;
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12: count = 31; break;
                case 4:
                case 6:
                case 9:
                case 11: count = 30; break;
            }

            return count;

        }

        /// <summary>
        /// 是否是闰年
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public static bool IsLeapYear(int year)
        {
            bool flag;
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            {
                flag = true;
            }
            else
                flag = false;
            return flag;
        }


        #region 获取 本周、本月、本季度、本年 的开始时间或结束时间
        /// <summary>
        /// 获取 本周、本月、本季度、本年 的开始时间
        /// </summary>
        /// <param name="TimeType">Week、Month、Season、Year</param>
        /// <param name="now"></param>
        /// <returns></returns>
        public static DateTime? GetTimeStartByType(string TimeType, DateTime now)
        {
            switch (TimeType)
            {
                case "Week":
                    return now.AddDays(-(int)now.DayOfWeek + 1);
                case "Month":
                    return now.AddDays(-now.Day + 1);
                case "Season":
                    var time = now.AddMonths(0 - ((now.Month - 1) % 3));
                    return time.AddDays(-time.Day + 1);
                case "Year":
                    return now.AddDays(-now.DayOfYear + 1);
                default:
                    return null;
            }
        }

        /// <summary>
        /// 获取 本周、本月、本季度、本年 的结束时间
        /// </summary>
        /// <param name="TimeType">Week、Month、Season、Year</param>
        /// <param name="now"></param>
        /// <returns></returns>
        public static DateTime? GetTimeEndByType(string TimeType, DateTime now)
        {
            switch (TimeType)
            {
                case "Week":
                    return now.AddDays(7 - (int)now.DayOfWeek);
                case "Month":
                    return now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
                case "Season":
                    var time = now.AddMonths((3 - ((now.Month - 1) % 3) - 1));
                    return time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1);
                case "Year":
                    var time2 = now.AddYears(1);
                    return time2.AddDays(-time2.DayOfYear);
                default:
                    return null;
            }
        }
        #endregion

    }
}
